package com.cuanbo.dualscreenteachzhu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cuanbo.dualscreenteachzhu.R;
import com.cuanbo.dualscreenteachzhu.bean.DrawBoradPage;
import com.cuanbo.dualscreenteachzhu.bean.EventBusBean;
import com.cuanbo.dualscreenteachzhu.bean.MyPath;
import com.cuanbo.dualscreenteachzhu.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class DrawBoardPageLayout extends LinearLayout {
    private List<DrawBoradPage> pages;

    public DrawBoardPageLayout(Context context) {
        super(context,null);
    }

    public DrawBoardPageLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawBoardPageLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, 0,0);
    }

    public DrawBoardPageLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        EventBus.getDefault().register(this);
        setPadding(DensityUtil.dp2px(10),DensityUtil.dp2px(10),DensityUtil.dp2px(10),DensityUtil.dp2px(10));
        setOrientation(LinearLayout.VERTICAL);
    }

    public void bindData(){
        if(pages==null)return;
        removeAllViews();
        for (int i=0;i<pages.size();i++){
            DrawBoradPage page=pages.get(i);
            TextView tv=new TextView(getContext());
            tv.setText(""+(i+1));
            tv.setTextColor(0xffffffff);
            tv.setTextSize(16);
            tv.setTag(i);
            tv.setGravity(Gravity.CENTER);
            tv.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    int index= (int) v.getTag();
                    for (int j=0;j<pages.size();j++){
                        DrawBoradPage p=pages.get(j);
                        if(j==index){
                            p.setCheck(true);
                        }else{
                            p.setCheck(false);
                        }
                    }
                    EventBusBean ebb=new EventBusBean(EventBusBean.DRAWBOARD_PAGE);
                    ebb.setData(pages);
                    EventBus.getDefault().post(ebb);
                }
            });
            if(page.isCheck()){
                tv.setBackgroundResource(R.drawable.page_ck);
            }else{
                tv.setBackgroundResource(R.drawable.page);
            }
            LayoutParams lp=new LayoutParams(DensityUtil.dp2px(25),DensityUtil.dp2px(25));
            lp.gravity = Gravity.CENTER_HORIZONTAL;
            if(i!=0) {
                lp.topMargin = DensityUtil.dp2px(5);
            }
            tv.setLayoutParams(lp);
            addView(tv);
        }
        TextView tv=new TextView(getContext());
        tv.setText("+");
        tv.setTextColor(0xffffffff);
        tv.setTextSize(20);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundResource(R.drawable.page);
        LayoutParams lp=new LayoutParams(DensityUtil.dp2px(25),DensityUtil.dp2px(25));
        lp.gravity = Gravity.CENTER_HORIZONTAL;
        lp.topMargin = DensityUtil.dp2px(5);
        tv.setLayoutParams(lp);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DrawBoradPage page=new DrawBoradPage();
                page.setCheck(false);
                List<MyPath> lines=new ArrayList<>();
                page.setLines(lines);
                pages.add(page);
                bindData();
            }
        });
        addView(tv);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventBusBean eventBusBean) {
        if(eventBusBean.getType()== EventBusBean.DRAWBOARD_PAGE){
            pages = (List<DrawBoradPage>) eventBusBean.getData();
            bindData();
        }else if(eventBusBean.getType()== EventBusBean.BUTTON_COMMENTS_OFF){
            setVisibility(View.GONE);
        }else if(eventBusBean.getType()== EventBusBean.BUTTON_COMMENTS_NO){
            setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

}
